package mymobile.diagram.navigator;

import mymobile.diagram.edit.parts.AppMobileAppNameEditPart;
import mymobile.diagram.edit.parts.AppMobileAppSettingEditPart;
import mymobile.diagram.edit.parts.AppMobileEditPart;
import mymobile.diagram.edit.parts.AppMobileMainViewEditPart;
import mymobile.diagram.edit.parts.ContainerEditPart;
import mymobile.diagram.edit.parts.ImageEditPart;
import mymobile.diagram.edit.parts.ImageNameEditPart;
import mymobile.diagram.edit.parts.InputEditPart;
import mymobile.diagram.edit.parts.InputNameEditPart;
import mymobile.diagram.edit.parts.LabelEditPart;
import mymobile.diagram.edit.parts.LabelNameEditPart;
import mymobile.diagram.edit.parts.ListEditPart;
import mymobile.diagram.edit.parts.ListNameEditPart;
import mymobile.diagram.edit.parts.SettingEditPart;
import mymobile.diagram.edit.parts.SettingSettingNameEditPart;
import mymobile.diagram.edit.parts.ViewEditPart;
import mymobile.diagram.edit.parts.ViewNextViewsEditPart;
import mymobile.diagram.edit.parts.ViewViewComponentsEditPart;
import mymobile.diagram.edit.parts.ViewViewNameEditPart;
import mymobile.diagram.edit.parts.ViewViewSettingEditPart;
import mymobile.diagram.part.MyMobileDiagramEditorPlugin;
import mymobile.diagram.part.MyMobileVisualIDRegistry;
import mymobile.diagram.providers.MyMobileElementTypes;
import mymobile.diagram.providers.MyMobileParserProvider;

import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class MyMobileNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		MyMobileDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		MyMobileDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof MyMobileNavigatorItem
				&& !isOwnView(((MyMobileNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof MyMobileNavigatorGroup) {
			MyMobileNavigatorGroup group = (MyMobileNavigatorGroup) element;
			return MyMobileDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof MyMobileNavigatorItem) {
			MyMobileNavigatorItem navigatorItem = (MyMobileNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (MyMobileVisualIDRegistry.getVisualID(view)) {
		case ContainerEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?ar.edu.unlp?Container", MyMobileElementTypes.Container_1000); //$NON-NLS-1$
		case SettingEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?ar.edu.unlp?Setting", MyMobileElementTypes.Setting_2010); //$NON-NLS-1$
		case LabelEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?ar.edu.unlp?Label", MyMobileElementTypes.Label_2009); //$NON-NLS-1$
		case ImageEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?ar.edu.unlp?Image", MyMobileElementTypes.Image_2011); //$NON-NLS-1$
		case AppMobileMainViewEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?ar.edu.unlp?AppMobile?mainView", MyMobileElementTypes.AppMobileMainView_4005); //$NON-NLS-1$
		case AppMobileEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?ar.edu.unlp?AppMobile", MyMobileElementTypes.AppMobile_2008); //$NON-NLS-1$
		case ViewViewSettingEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?ar.edu.unlp?View?viewSetting", MyMobileElementTypes.ViewViewSetting_4001); //$NON-NLS-1$
		case ViewNextViewsEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?ar.edu.unlp?View?nextViews", MyMobileElementTypes.ViewNextViews_4004); //$NON-NLS-1$
		case ViewEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?ar.edu.unlp?View", MyMobileElementTypes.View_2005); //$NON-NLS-1$
		case ListEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?ar.edu.unlp?List", MyMobileElementTypes.List_2007); //$NON-NLS-1$
		case AppMobileAppSettingEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?ar.edu.unlp?AppMobile?appSetting", MyMobileElementTypes.AppMobileAppSetting_4003); //$NON-NLS-1$
		case InputEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?ar.edu.unlp?Input", MyMobileElementTypes.Input_2006); //$NON-NLS-1$
		case ViewViewComponentsEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?ar.edu.unlp?View?viewComponents", MyMobileElementTypes.ViewViewComponents_4002); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = MyMobileDiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& MyMobileElementTypes.isKnownElementType(elementType)) {
			image = MyMobileElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof MyMobileNavigatorGroup) {
			MyMobileNavigatorGroup group = (MyMobileNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof MyMobileNavigatorItem) {
			MyMobileNavigatorItem navigatorItem = (MyMobileNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (MyMobileVisualIDRegistry.getVisualID(view)) {
		case ContainerEditPart.VISUAL_ID:
			return getContainer_1000Text(view);
		case SettingEditPart.VISUAL_ID:
			return getSetting_2010Text(view);
		case LabelEditPart.VISUAL_ID:
			return getLabel_2009Text(view);
		case ImageEditPart.VISUAL_ID:
			return getImage_2011Text(view);
		case AppMobileMainViewEditPart.VISUAL_ID:
			return getAppMobileMainView_4005Text(view);
		case AppMobileEditPart.VISUAL_ID:
			return getAppMobile_2008Text(view);
		case ViewViewSettingEditPart.VISUAL_ID:
			return getViewViewSetting_4001Text(view);
		case ViewNextViewsEditPart.VISUAL_ID:
			return getViewNextViews_4004Text(view);
		case ViewEditPart.VISUAL_ID:
			return getView_2005Text(view);
		case ListEditPart.VISUAL_ID:
			return getList_2007Text(view);
		case AppMobileAppSettingEditPart.VISUAL_ID:
			return getAppMobileAppSetting_4003Text(view);
		case InputEditPart.VISUAL_ID:
			return getInput_2006Text(view);
		case ViewViewComponentsEditPart.VISUAL_ID:
			return getViewViewComponents_4002Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getContainer_1000Text(View view) {
		mymobile.mobileTest.Container domainModelElement = (mymobile.mobileTest.Container) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getAppName();
		} else {
			MyMobileDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSetting_2010Text(View view) {
		IParser parser = MyMobileParserProvider.getParser(
				MyMobileElementTypes.Setting_2010,
				view.getElement() != null ? view.getElement() : view,
				MyMobileVisualIDRegistry
						.getType(SettingSettingNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			MyMobileDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5010); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getLabel_2009Text(View view) {
		IParser parser = MyMobileParserProvider.getParser(
				MyMobileElementTypes.Label_2009,
				view.getElement() != null ? view.getElement() : view,
				MyMobileVisualIDRegistry.getType(LabelNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			MyMobileDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5009); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getImage_2011Text(View view) {
		IParser parser = MyMobileParserProvider.getParser(
				MyMobileElementTypes.Image_2011,
				view.getElement() != null ? view.getElement() : view,
				MyMobileVisualIDRegistry.getType(ImageNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			MyMobileDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5011); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getAppMobileMainView_4005Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getAppMobile_2008Text(View view) {
		IParser parser = MyMobileParserProvider.getParser(
				MyMobileElementTypes.AppMobile_2008,
				view.getElement() != null ? view.getElement() : view,
				MyMobileVisualIDRegistry
						.getType(AppMobileAppNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			MyMobileDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5008); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getViewViewSetting_4001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getViewNextViews_4004Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getView_2005Text(View view) {
		IParser parser = MyMobileParserProvider.getParser(
				MyMobileElementTypes.View_2005,
				view.getElement() != null ? view.getElement() : view,
				MyMobileVisualIDRegistry
						.getType(ViewViewNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			MyMobileDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getList_2007Text(View view) {
		IParser parser = MyMobileParserProvider.getParser(
				MyMobileElementTypes.List_2007,
				view.getElement() != null ? view.getElement() : view,
				MyMobileVisualIDRegistry.getType(ListNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			MyMobileDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5007); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getAppMobileAppSetting_4003Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getInput_2006Text(View view) {
		IParser parser = MyMobileParserProvider.getParser(
				MyMobileElementTypes.Input_2006,
				view.getElement() != null ? view.getElement() : view,
				MyMobileVisualIDRegistry.getType(InputNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			MyMobileDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getViewViewComponents_4002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return ContainerEditPart.MODEL_ID.equals(MyMobileVisualIDRegistry
				.getModelID(view));
	}

}
